package com.atguigu.com.nio.nio_2_channel;

import sun.nio.ch.FileChannelImpl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @Author: robert.song
 * @Date: Created in 2021/8/6
 */
public class NIOFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str = "hello,atguigu";
//        创建输出流->channel
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\file\\project\\cx\\test.log");
//        通过输fileOutputStream获取对应的FileChannelImpl实现类，
        FileChannelImpl fileChannel = (FileChannelImpl) fileOutputStream.getChannel();
//        创建一个缓冲区 ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//        将str放入byteBuffer
        byteBuffer.put(str.getBytes());
//        对byteBuffer进行反转
        byteBuffer.flip();
//        将byteBuffer数据写入到fileChannel
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
    }
}
